/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.infoq.bambook;

import com.sdo.bambooksdk.BambookCore;
import com.sdo.bambooksdk.Const;
import com.sdo.bambooksdk.PrivBookInfo;
import com.sdo.bambooksdk.TransCallback;
import com.sun.jna.ptr.IntByReference;
import java.util.Date;

/**
 *
 * @author zhangkf
 */
public class InfoQBambookHelper {

    public static int getConn() {
        String lpszIP = "192.168.250.2";
        int timeOut = 0;
        IntByReference pConn = new IntByReference();
        int expResult = Const.BR_SUCC;
        int result = BambookCore.INSTANCE.BambookConnect(lpszIP, timeOut, pConn);
        System.out.println("Connect result: "+result);
        if (expResult == result) {
            return pConn.getValue();
        } else {
            return 0;
        }
    }

    public static void disConn(int hConn) {
        int result = BambookCore.INSTANCE.BambookDisconnect(hConn);
        System.out.println("Disconnect result: "+result);
    }

    public static void pack(String bookName) {
        System.out.println("begin packing...");
        String pszSnbFile = ".\\"+bookName;
        int result = BambookCore.INSTANCE.BambookPackSnbFromDir(pszSnbFile, ".\\infoq_bambook");
        System.out.println("pack result: "+result);
        result = BambookCore.INSTANCE.BambookVerifySnbFile(pszSnbFile);
        System.out.println("verify result: "+result);
    }

    public static void upload(String bookName) {
        int hConn = getConn();
        String pszSnbFile = ".\\"+bookName;

        TransCallback pCallbackFunc = new TransCallback() {

            public void invoke(int status, int progress, int userData) {
                //do nothing
            }
        };

        int userData = 0;

        int result = BambookCore.INSTANCE.BambookAddPrivBook(hConn, pszSnbFile, pCallbackFunc, userData);
        System.out.println("Add book result: "+result);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
        }

        PrivBookInfo pInfo = new PrivBookInfo();
        pInfo.cbSize = pInfo.size();
        result = BambookCore.INSTANCE.BambookGetFirstPrivBookInfo(hConn, pInfo);
        System.out.println("Get first priv book result: "+result);
        disConn(hConn);
    }
    
}
